package com.jiukuaidao.zhulin.module.commodity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;

import com.baidu.mapapi.map.Text;
import com.jiukuaidao.library.Config;
import com.jiukuaidao.library.base.BaseActivity;
import com.jiukuaidao.library.base.BaseHolder;
import com.jiukuaidao.library.base.BaseXrecyclerViewAdapter;
import com.jiukuaidao.library.widget.recyclerview.XRecyclerView;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.entity.ApiGoodsCommentsInfo;
import com.jiukuaidao.zhulin.module.commodity.contract.GoodsCommentsContract;
import com.jiukuaidao.zhulin.module.commodity.presenter.GoodsCommentsPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wangyijie on 16.8.3.
 */

public class GoodsCommentsActivity extends BaseActivity<GoodsCommentsContract.Presenter> implements GoodsCommentsContract.View,XRecyclerView.OnRefreshListener,View.OnClickListener{

    @BindView(R.id.commodity_goods_comments_view)
    XRecyclerView goodsCommentsXRecyclerView;
    @BindView(R.id.commodity_goods_comments_title)
    TextView titleView;
    @BindView(R.id.commodity_goods_comments_back)
    TextView titleBack;

    private List<ApiGoodsCommentsInfo.UserComment> commentList;

    private BaseXrecyclerViewAdapter goodsCommentsAdapter;

    private String goodsId;//商品id
    private int pageIndex = 1;//

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ButterKnife.bind(this);

    }

    @Override
    protected View getTargetView() {
        return null;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_commodity_goods_comments;
    }

    @Override
    protected void handleBundle(Bundle bundle) {
        goodsId = bundle.getString("goodsId");
    }

    @Override
    protected void init() {
        initRecyclerView();
        new GoodsCommentsPresenter(TaskProvider.provideTasksRepository(this), this);
        titleBack.setClickable(true);
        titleBack.setOnClickListener(this);
        goodsCommentsXRecyclerView.setOnRefreshListener(this);
        goodsCommentsXRecyclerView.setLoadingMoreEnabled(true);

        mPresenter.start();
    }

    private void initRecyclerView(){
        goodsCommentsXRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        goodsCommentsAdapter = new BaseXrecyclerViewAdapter<ApiGoodsCommentsInfo.UserComment>(Config.mContext,R.layout.item_commodity_goods_comments,this.commentList,goodsCommentsXRecyclerView) {
            @Override
            public void updateUI(BaseHolder holder, int position, ApiGoodsCommentsInfo.UserComment data) {

                TextView tvMobile = holder.getView(R.id.commodity_goods_comments_mobile);
                TextView tvComment = holder.getView(R.id.commodity_goods_comments_user_comment);
                RatingBar rb = holder.getView(R.id.commodity_goods_comments_rbar);
//                rb.setNumStars(data.userGrade);
                rb.setRating(data.userGrade);
                String starMobile = data.mobile.substring(0,3) + "****" + data.mobile.substring(7,11);
                tvMobile.setText(starMobile);
                tvComment.setText(data.userComment==null?"":data.userComment);

            }


        };
        goodsCommentsXRecyclerView.setAdapter(goodsCommentsAdapter);
    }

    @Override
    public String getGoodsId() {
        return goodsId;
    }

    @Override
    public String getPageIndex() {
        return String.valueOf(pageIndex);
    }

    @Override
    public void getGoodsCommentSuc(ApiGoodsCommentsInfo info,int dataAction) {

        switch (dataAction){

            case GoodsCommentsContract.REFRESH:
                //刷新操作
                goodsCommentsXRecyclerView.refreshComplete();
                this.commentList = info.list;
                this.goodsCommentsAdapter.setData(commentList);
                this.goodsCommentsAdapter.notifyDataSetChanged();
                break;

            case GoodsCommentsContract.LOADMORE:
                //加载更多
                goodsCommentsXRecyclerView.loadMoreComplete();
                if (this.commentList == null){
                    this.commentList = new ArrayList<>();
                }
                this.commentList.addAll(info.list);
                this.goodsCommentsAdapter.setData(commentList);
                this.goodsCommentsAdapter.notifyDataSetChanged();
                break;

        }

//        this.commentList = info.list;
//        this.goodsCommentsAdapter.setData(commentList);
//        this.goodsCommentsAdapter.notifyDataSetChanged();

    }

    @Override
    public void onRefresh() {

        pageIndex = 1;

        mPresenter.loadGoodsComment(GoodsCommentsContract.REFRESH);

    }

    @Override
    public void onLoadMore() {

        pageIndex++;

        mPresenter.loadGoodsComment(GoodsCommentsContract.LOADMORE);

    }

    @OnClick({R.id.commodity_goods_comments_back})
    public void onClick(View v){
        switch (v.getId()){

            case R.id.commodity_goods_comments_back:
                this.finish();
                break;
        }

    }
}
